package com.shop.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionContext;
import com.shop.pojo.Book;
import com.shop.pojo.Booktype;
import com.shop.pojo.Orderlist;
import com.shop.pojo.Price;
import com.shop.pojo.Users;

@Transactional
public class BookService extends SysService
{

	public List<Book> findBooks(Book book, Integer pageNum, Integer rows)
	{
		String hql = "from Book book  where 1=1";
		List list1 = new ArrayList();
		hql = changeHql(book, hql, list1);
		hql += " order by book.booksales desc";
		List<Book> books = getBookDao().find(hql, list1, pageNum, rows);
		System.out.println(hql);
		return books;
	}

	public long getTotal(Book book)
	{
		List list = new ArrayList();
		String hql = "select count(*) from Book book where 1=1";
		hql = changeHql(book, hql, list);
		hql += " order by book.booksales desc";
		System.out.println(hql);
		return getBookDao().count(hql, list);
	}

	public String changeHql(Book book, String hql, List list2)
	{

		if (book.getBookauthor()!= null
				&&(book.getBookauthor().trim().length()>0))
			{
				System.out.println("bbbbbbbbbbbb"+(book.getBookauthor().trim().length()>0));
				hql += "  and book.bookauthor like ? ";
				list2.add("%" + book.getBookauthor() + "%");

			}
		if (book.getBookname() != null
				&& book.getBookname().trim().length()>0)
			{
				hql += " and book.bookname like ? ";
				list2.add("%" + book.getBookname() + "%");

			}
		if (book.getPublish() != null && book.getPublish().trim().length()>0)
			{
				hql += " and book.publish like ? ";
				list2.add("%" + book.getPublish() + "%");

			}
		if (book.getBooktype()!= null
				&& book.getBooktype().getIdBooktype().trim().length()>0)
			{
				hql += " and book.booktype.idBooktype=?";
				list2.add(book.getBooktype().getIdBooktype());

			}
		System.out.println(hql);
		return hql;
	}

	public boolean buy(String idBook)
	{
		Map map = ActionContext.getContext().getSession();

		Users user = (Users) map.get("user");
		BigDecimal singleprice = null;
		Set<Orderlist> car = (Set<Orderlist>) map.get("car");
		if (car == null)
			{
				car = new HashSet<Orderlist>();
				map.put("car", car);
			}
		Iterator<Orderlist> itr = car.iterator();
		while (itr.hasNext())
			{
				Orderlist ol = itr.next();
				if (ol.getBook().getIdBook().equals(idBook))
					{
						ol.setOlcount(new BigDecimal(
								ol.getOlcount().intValue() + 1));
						return true;
					}
			}
		Book book = (Book) getBookDao().get(Book.class, idBook);
		Set<Price> prices = book.getPrices();
		for (Price price : prices)
			{
				if (user.getUtype().getIdUtype()
						.equals(price.getUtype().getIdUtype()))

					{
						singleprice = price.getRealprice();

					}

			}

		Orderlist ol = new Orderlist();
		ol.setIdOl(UUID.randomUUID().toString());
		ol.setOlno("olno" + UUID.randomUUID().toString());
		ol.setBook(book);
		ol.setOlcount(new BigDecimal(1));
		ol.setSaleprice(new BigDecimal(singleprice.doubleValue()
				* ol.getOlcount().doubleValue()));
		car.add(ol);
		return true;

	}

	public boolean changeSize(String idBook, Integer num)
	{
		Map map = ActionContext.getContext().getSession();

		Set<Orderlist> car = (Set<Orderlist>) map.get("car");
		if (car == null)
			{

				car = new HashSet<Orderlist>();
				map.put("car", car);
			}
		Iterator itr = car.iterator();
		while (itr.hasNext())
			{

				Orderlist ol = (Orderlist) itr.next();
				if (ol.getBook().getIdBook().equals(idBook))
					{
						ol.setOlcount(new BigDecimal(ol.getOlcount().intValue()
								+ num));
						if (ol.getOlcount().intValue() <= 0)
							{
								ol.setOlcount(new BigDecimal(0));
								car.remove(ol);

							}
						return true;
					}

			}
		return true;

	}

	public List<Booktype> getTypes()
	{

		return getBookTypeDao().findAll(Booktype.class);
	}

}
